( function ( D ) {
    D.newLine = function ( collectionId ) 
    {
        var container = document.createElement( "div" );
        container.setAttribute( "class", "line" );

        var controls = document.createElement( "div" );
        controls.setAttribute( "class", "controls" );

	var collectionLabel = document.createElement( "span" );
	controls.appendChild(collectionLabel);

	var closeLine = D.makeControl("fermer la ligne", "&#x2613;");
        var swapNextLine = D.makeControl("échanger avec la ligne suivante", "&#x2193;");
        var swapPrevLine = D.makeControl("échanger avec la ligne précédente", "&#x2191;");
        var backward = D.makeControl("afficher la série de pages précédente", "&#x21D0;");
        var backwardByOne = D.makeControl("afficher une page précédente", "&#x2190;");
        var forwardByOne = D.makeControl("afficher une page suivante", "&#x2192;");
        var forward = D.makeControl("afficher la série de pages suivantes", "&#x21D2;");
        var deleteView = D.makeControl("supprimer cette vue", "SUPPR");
        deleteView.style.display = "none";
        var dynamicTree = D.makeControl("naviguer dans les inserts", "sous-collections");
	var switchFiles = D.makeControl("montrer/cacher les dossiers", "dossiers");

        var nbImages = document.createElement( "select" );
        nbImages.setAttribute( "size", "1" );
        nbImages.setAttribute( "class", "control" );
        nbImages.setAttribute( "title", "choisir le nombre d'images à afficher" );

        var options = [ ];
        for( var i = 1 ; i < 9 ; i += 1 ) 
        {
            options[ i ] = document.createElement( "option" );
            options[ i ].setAttribute( "value", i );
            options[ i ].innerHTML = i;
            nbImages.appendChild( options[ i ] );
        }
        options[ D.defaultNbImages ].setAttribute( "selected", "true" );

        var grid = document.createElement( "ul" );
        grid.setAttribute( "class", "grid" );

        container.appendChild( controls         );
        container.appendChild( grid             );
        controls.appendChild ( closeLine        );
        controls.appendChild ( swapPrevLine     );
        controls.appendChild ( swapNextLine     );
        controls.appendChild ( backward         );
        controls.appendChild ( backwardByOne    );
        controls.appendChild ( nbImages         );
        controls.appendChild ( forwardByOne     );
        controls.appendChild ( forward          );
        controls.appendChild ( deleteView       );
        controls.appendChild ( dynamicTree      );
        controls.appendChild ( switchFiles      );

        D.disableSelection( container );

        var lines = document.getElementById( "lines" );
        lines.appendChild( container );

        var line = {};

	line.id = collectionId;
	line.images = [];


        line.firstVisibleImage = 0;

        line.nbImages = D.defaultNbImages;

	line.setImages = function(images){
		line.images = images;
	}

	line.gotoPage = function(numPage){
		line.firstVisibleImage = numPage;
		D.updateImages(line);
	}

        line.addPage = function( page )
        {
		line.images.push(page);
        };

        line.grid = grid;

        line.container = container;
	line.switchFiles = switchFiles;

        closeLine.onclick = function ( e ) 
        {
            lines.removeChild( container );
            e.stopPropagation( );
            return false;
        }

        swapPrevLine.onclick = function ( e ) 
        {
            var previousLine = container.previousSibling;

            if( previousLine )
            {
                lines.insertBefore( lines.removeChild( previousLine ), container.nextSibling );
            }
            e.stopPropagation( );
            return false;
        }

        swapNextLine.onclick = function ( e ) 
        {
            var nextLine = container.nextSibling;

            if( nextLine )
            {
                lines.insertBefore( lines.removeChild( nextLine ), container );
            }
            e.stopPropagation( );
            return false;
        }

        nbImages.onchange = function ( ) 
        {
            line.nbImages = parseInt( this.selectedValue( ) );
            D.updateImages( line );
        }

        var forwardBy = function( n ) 
        {

            if( line.firstVisibleImage + 
                line.nbImages + 
                n > 
                line.images.length )
            {
                return false;
            }

            line.firstVisibleImage += n;
            D.updateImages( line );
            return false;
        }

        var backwardBy = function( n ) 
        {

            if( line.firstVisibleImage - n < 0 )
            {
                return false;
            }

            line.firstVisibleImage -= n;
            D.updateImages( line );
            return false;
        }

        var forwardOneScreen = function( e ) 
        {
            e.stopPropagation( );
            return forwardBy( line.nbImages );
        }

        var backwardOneScreen = function( e ) 
        {
            e.stopPropagation( );
            return backwardBy( line.nbImages );
        }

        forwardByOne.onclick  = function( e ) 
        {
            e.stopPropagation( );
            return forwardBy( 1 );
        }

        backwardByOne.onclick = function( e ) 
        {
            e.stopPropagation( );
            return backwardBy(1)
        }

        forward.onclick = forwardOneScreen;

        backward.onclick = backwardOneScreen;

        deleteView.onclick = function( e )
        {
            e.stopPropagation( );
            if( line.isView && confirm( "Supprimer la vue " + line.id + " ?" ) )
            {
        	D.request("deleteCollection.txt", 
        		{
        	    		"collectionId": line.id 
        		},
        		function(result)
        		{
        	    		D.request("deleteCollection1.txt", 
        	    			{
						"collectionId": line.id,
                                		"timestamp": new Date( ).getTime( ),
                                		"user": D.userName( )
					}, null, true 
				);
        		}, true 
		);
                lines.removeChild( container );
            }
            return false;
        }

        dynamicTree.onclick = function( e )
        {
            if( !line.isView )
            {
                var collectionName = line.id.replace( /^.*collections\//, "" ),
                    url = self.location.protocol + "//" + self.location.hostname + 
                          "/collections/" + collectionName + "/navigation.html",
                    treeWindow = window.open( url, "dynamicTree" );

                treeWindow.focus( );
            }

            return false;
        }

        container.onmousedown = function( e ) 
        {
            D.dragObject = line;
	    D.dragObject.type = "line";
	    if(!line.isView){
	    	D.dragObject.id = line.id;
	    	D.dragObject.thumb = line.images[0].thumb;
	    	D.dragObject.big = line.images[0].big;
	    }
        }

        container.onmouseup = function ( e ) 
        {
            if( D.dragObject.type === "page" && line.isView )
            {
		var dragObject = D.dragObject;
		var pageId = dragObject.line.images[ dragObject.i ].id;
		
		D.request( "addPageToCollection.txt",
		    {
			"collectionId": line.id,
			"page": pageId,
			"numPage": line.images.length + 1,
			"timestamp": new Date( ).getTime( ),
			"user": D.userName( )
		    }, 
		    function( )
		    {
			line.addPage( dragObject.line.images[ dragObject.i ] );
			D.updateImages( line ); 
		    }, true );
            }
            else if( D.dragObject.type === "line" ) 
            {
                var sourceImages = D.dragObject.images,
		    lastPageNum = line.images.length + 1,
		    insertImages = "";

                if( line.isView && !D.dragObject.isView ) 
                {
                    if( confirm( "Ajouter la collection ?" ) ) 
                    {
			for(var i=0; i<sourceImages.length; i+=1){
				insertImages += "<"+line.id+"/pages> rdf:_"+
					(i+lastPageNum+1)+" <"+sourceImages[i].id+"> .\n"+
					"<"+sourceImages[i].id+"> cols:collection <"+line.id+"> .\n";
			}

                	D.request( "insertCollection.txt",
                	{
                	    "user": D.userName(),
                	    "sourceCollectionId": D.dragObject.id,
                	    "destinationCollectionId": line.id,
                	    "insertImages": insertImages,
                	    "timestamp": new Date( ).getTime( )
                	}, null, true );

                        for( var i = 0 ; i < sourceImages.length ; i += 1 ) 
                        {
                            line.images.push(sourceImages[i]);
                        }

                        D.updateImages( line );
                    }
                }
            }
        }

	D.request("collectionPerso.txt",
		{
			"collectionId": collectionId
		},
		function(results){
			line.isView = results.boolean;
			if(line.isView){
        			deleteView.style.display = "inline";
			}
		}, false, true
	);

	D.request("getCollectionLabel.txt",
		{
			"collectionId": collectionId
		},
		function(results){
			var label;
			if(results.length === 1){
				label = results[0].collectionLabel.value;
				if(results[0].parentCollectionLabel){
					label = results[0].parentCollectionLabel.value + "->" + label;
				}
				collectionLabel.innerHTML = decodeURIComponent(label);
			}
		}
	);

	D.request("getCollectionImages.txt",
		{
			"collectionId": collectionId
		},
		function(results){
			var pages = {};
			var keys = [];
			var key;

			for(var i=0; i<results.length; i+=1){
				key = results[i].numPage.value.replace(/^.*rdf-syntax-ns#_/, "");
				pages[key] = {
				    "id": results[i].pageId.value,
				    "big": results[i].big.value,
				    "bigWidth": results[i].bigWidth.value,
				    "bigHeight": results[i].bigHeight.value,
				    "thumb": results[i].thumb.value,
				    "thumbWidth": results[i].thumbWidth.value,
				    "thumbHeight": results[i].thumbHeight.value
				};
				keys[i] = parseInt(key); 
			}
			keys.sort(function(a,b){ return a-b; });
			for(var i=0; i<keys.length; i+=1){
				line.images[i] = pages[keys[i].toString()];
			}
			D.updateImages(line);
		}
	);

	D.initFiles(line);

        return line;

    };
} ) ( DESANTI );
